
clear

set mem 100m
set matsize 4000
set more off

local evt_window = 10 /*Define the width of event dates to include as event day dummies*/
					/*Vary 20, 10, 5 for robustness check*/
local estwindow = 180
local evtwindow = 30
local EVENT  "compevent" /*decevent, compevent, or debtreliefevent*/
local hipc  "all" /*hipc: original HIPC, ehipc: enhanced HIPC, or all*/
local thintrade    = 0 /*0 (No thin trade) or 1*/
local controlevents = 1
local controlindustry = 1


use data_event_study_before_estimation_T_`estwindow'_`evtwindow'_thin`thintrade'_`EVENT', clear



///DROP SAMPLES WITH SHORT ESTIMATION WINDOW
egen tmp1 = min(event_time), by(panel_id)
drop if tmp1>-60 
drop tmp*
drop if event_time<-122 | event_time==. /*To be comparable to the portfolio approach*/


if "`EVENT'"=="decevent" {
	if "`hipc'" == "hipc" {
		keep if dechipc==1
	}
	if "`hipc'" == "ehipc" {
		keep if decehipc==1
	}
}
if "`EVENT'"=="compevent" {
	if "`hipc'" == "hipc" {
		keep if comphipc==1
	}
	if "`hipc'" == "ehipc" {
		keep if compehipc==1
	}
}
if "`EVENT'"=="debtreliefevent" {
	if "`hipc'" == "hipc" {
		keep if debtreliefhipc==1
	}
	if "`hipc'" == "ehipc" {
		keep if debtreliefehipc==1
	}
}



if `controlindustry'==1 {
        if `thintrade'==0 {
            local industry "R_group"
        }
        else if `thintrade'==1 {
            local industry "Rindtt"
        }
}


local action ""
replace corporate_action = "0" if corporate_action==""


*Create event time dummies according to the desired window

gen year1= year(date) 
replace year=0 if year==.

gen month = month(date) 
replace month=0 if month==.

gen day1 = day 
replace day1=0 if day1==.

gen Dmonth= mofd(date)
gen Dweek= wofd(date)


if `controlindustry'==1 {
	local industrycontrols "i.panel_id|`industry'"
}
else if `controlindustry'==0 {
	local industrycontrols ""
}



tab event_time if abs(event_time)<=`evt_window' & event_window==1, gen(Devent)
local neventdummies = 2*`evt_window'+1
forv i==1/`neventdummies' {
		replace Devent`i' = 0 if Devent`i' == .
	}

drop if event_time > `evt_window'

*exit


/*Robust check of the length of the estimation window*/
su event_time
local mintime = r(min)
local maxtime = -`evt_window' - 50
local robust = `maxtime'-`mintime'+ 1 /*Case for baseline estimation window only*/


matrix A1 = J(`neventdummies',floor((`maxtime'-`mintime')/`robust'+1)*3,.)		


*=======================================================================================================
*REGRESSIONS with all coefficients panel_id specific (except event time dummies)
*=======================================================================================================

*I. NO-THIN TRADE ISSUES
*-------------------------------------------------------------------------------------------------------

if `thintrade'== 0 {
*================================================================================
*1. Baseline specification (i.e., no addiitonal time dummy variables)
*================================================================================
local m = 0

forv n==`mintime'(`robust')`maxtime' {

	xi: reg  R  i.panel_id*Rm  `industrycontrols'  i.panel_id*i.corporate_action  Devent* if event_time>=`n', cluster(date) r

	local diff10 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14]+_b[Devent15]+_b[Devent16]+_b[Devent17]+_b[Devent18]+_b[Devent19]+_b[Devent20]+_b[Devent21])/11 ///
			  - (_b[Devent1]+_b[Devent2]+_b[Devent3]+_b[Devent4]+_b[Devent5]+_b[Devent6]+_b[Devent7]+_b[Devent8]+_b[Devent9]+_b[Devent10])/10
	test (Devent1+Devent2+Devent3+Devent4+Devent5+Devent6+Devent7+Devent8+Devent9+Devent10)/10 ///
		= (Devent11+Devent12+Devent13+Devent14+Devent15+Devent16+Devent17+Devent18+Devent19+Devent20+Devent21)/11 
	local F1 = r(F)
	local p1 = r(p)

	local diff5 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14]+_b[Devent15]+_b[Devent16])/6 ///
			  - (_b[Devent6]+_b[Devent7]+_b[Devent8]+_b[Devent9]+_b[Devent10])/5
	test (Devent6+Devent7+Devent8+Devent9+Devent10)/5 = (Devent11+Devent12+Devent13+Devent14+Devent15+Devent16)/6
	local F2 = r(F)
	local p2 = r(p)

	local diff3 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14])/4 - (_b[Devent8]+_b[Devent9]+_b[Devent10])/3
	test (Devent8+Devent9+Devent10)/3 = (Devent11+Devent12+Devent13+Devent14)/4  

	
	if `m'==0 {
		outreg Devent* using Regression_Approach_Final_baseline_`EVENT'_`hipc'.txt, se 3aster coefast nolabel ctitle(`n') ///
		addstat(-10to-1vs0to10, `diff10', F test, `F1', Prob>F, `p1', -5to-1vs0to5, `diff5', F test, `F2', Prob>F, `p2', -3to-1vs0to3, `diff3', F test,  r(F), Prob>F, r(p)) ///
		adec(5, 3, 3, 5, 3, 3, 5, 3, 3) replace
	}
	else {
		outreg Devent* using Regression_Approach_Final_baseline_`EVENT'_`hipc'.txt, se 3aster coefast nolabel ctitle(`n') ///
		addstat(-10to-1vs0to10, `diff10', F test, `F1', Prob>F, `p1', -5to-1vs0to5, `diff5', F test, `F2', Prob>F, `p2', -3to-1vs0to3, `diff3', F test,  r(F), Prob>F, r(p)) ///
		adec(5, 3, 3, 5, 3, 3, 5, 3, 3) append
	}
	
	
	*Building CAR and their standard errors

	forv i==1/`neventdummies' {
		if `i'==1 {
			matrix A1[`i',`m'+1] = _b[Devent1]
			matrix A1[`i',`m'+2] = _se[Devent1]
			matrix A1[`i',`m'+3] = 2*ttail(e(df_r),abs(_b[Devent1]/_se[Devent1]))
		}
		else {
			local exp "Devent1"

			forv j == 2/`i' {
				local exp "`exp' + Devent`j'"
			}

			qui lincom `exp'
			matrix A1[`i',`m'+1] = r(estimate)
			matrix A1[`i',`m'+2] = r(se)
			matrix A1[`i',`m'+3] = 2*ttail(r(df),abs(r(estimate)/r(se)))
		}
	}

local m = `m'+3

}


}



*II. THIN TRADE ISSUES
*-------------------------------------------------------------------------------------------------------


if `thintrade'== 1 {

*================================================================================
*1. Baseline specification (i.e., no addiitonal time dummy variables)
*================================================================================
local m = 0

forv n==`mintime'(`robust')`maxtime' {

	xi: reg  Rtt  i.panel_id*ntt  i.panel_id|Rmtt  `industrycontrols'  i.panel_id*i.corporate_action  Devent* if event_time>=`n', cluster(date) r

	local diff10 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14]+_b[Devent15]+_b[Devent16]+_b[Devent17]+_b[Devent18]+_b[Devent19]+_b[Devent20]+_b[Devent21])/11 ///
			  - (_b[Devent1]+_b[Devent2]+_b[Devent3]+_b[Devent4]+_b[Devent5]+_b[Devent6]+_b[Devent7]+_b[Devent8]+_b[Devent9]+_b[Devent10])/10
	test (Devent1+Devent2+Devent3+Devent4+Devent5+Devent6+Devent7+Devent8+Devent9+Devent10)/10 ///
		= (Devent11+Devent12+Devent13+Devent14+Devent15+Devent16+Devent17+Devent18+Devent19+Devent20+Devent21)/11 
	local F1 = r(F)
	local p1 = r(p)

	local diff5 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14]+_b[Devent15]+_b[Devent16])/6 ///
			  - (_b[Devent6]+_b[Devent7]+_b[Devent8]+_b[Devent9]+_b[Devent10])/5
	test (Devent6+Devent7+Devent8+Devent9+Devent10)/5 = (Devent11+Devent12+Devent13+Devent14+Devent15+Devent16)/6
	local F2 = r(F)
	local p2 = r(p)

	local diff3 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14])/4 - (_b[Devent8]+_b[Devent9]+_b[Devent10])/3
	test (Devent8+Devent9+Devent10)/3 = (Devent11+Devent12+Devent13+Devent14)/4  

	if `m'==0 {
		outreg Devent* using Regression_Approach_Final_baseline_thin_`EVENT'_`hipc'.txt, se 3aster coefast nolabel ctitle(`n') ///
		addstat(-10to-1vs0to10, `diff10', F test, `F1', Prob>F, `p1', -5to-1vs0to5, `diff5', F test, `F2', Prob>F, `p2', -3to-1vs0to3, `diff3', F test,  r(F), Prob>F, r(p)) ///
		adec(5, 3, 3, 5, 3, 3, 5, 3, 3) replace
	}
	else {
		outreg Devent* using Regression_Approach_Final_baseline_thin_`EVENT'_`hipc'.txt, se 3aster coefast nolabel ctitle(`n') ///
		addstat(-10to-1vs0to10, `diff10', F test, `F1', Prob>F, `p1', -5to-1vs0to5, `diff5', F test, `F2', Prob>F, `p2', -3to-1vs0to3, `diff3', F test,  r(F), Prob>F, r(p)) ///
		adec(5, 3, 3, 5, 3, 3, 5, 3, 3) append
	}
	
	
	*Building CAR and their standard errors

	forv i==1/`neventdummies' {
		if `i'==1 {
			matrix A1[`i',`m'+1] = _b[Devent1]
			matrix A1[`i',`m'+2] = _se[Devent1]
			matrix A1[`i',`m'+3] = 2*ttail(e(df_r),abs(_b[Devent1]/_se[Devent1]))
		}
		else {
			local exp "Devent1"

			forv j == 2/`i' {
				local exp "`exp' + Devent`j'"
			}

			qui lincom `exp'
			matrix A1[`i',`m'+1] = r(estimate)
			matrix A1[`i',`m'+2] = r(se)
			matrix A1[`i',`m'+3] = 2*ttail(r(df),abs(r(estimate)/r(se)))
		}
	}

local m = `m'+3

}


}


mat list A1


egen id = group(panel_id)
keep if id==1
keep if event_time>=-10 & event_time<=10
keep event_time
svmat A1, names( col )


local max = floor((`maxtime'-`mintime')/`robust'+1)

forv i==1/`max' {
	local m1 = ( `i'-1)*3+1
	local m2 = ( `i'-1)*3+2
	local m3 = ( `i'-1)*3+3
	rename c`m1' car`i' 
	rename c`m2' se`i' 
	rename c`m3' p`i' 

	gen star`i' = car`i'  if p`i' <=0.1
}

di `max'

sort event_time

if `thintrade'== 0 {
	saveold Regression_Approach_CAR_baseline_`EVENT'_`hipc', replace
}
if `thintrade'== 1 {
	saveold Regression_Approach_CAR_baseline_thin_`EVENT'_`hipc', replace
}


use Regression_Approach_CAR_baseline`thin'_`EVENT'_`hipc', clear

forv n=1/21 {
	 replace event_time = `n'-11 in `n'
}

gen ciu = car1 + se1*1.64
gen cil = car1 - se1*1.64

twoway (line car1 event_time, lwidth(thick)) ///
(line ciu event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
, yline(0, lwidth(vvthin)) /*yscale(range(0 0.01))*/ ytitle(Cumulative abnormal return, margin(medium)) xtitle(Event time, margin(medium)) legend(off)

gr save Figure_8_B, replace 
